package com.plian.system.controller.pm.realproperty;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.pm.realproperty.RealPropertyQuo;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.pm.realproperty.IRealPropertyQuoService;
import com.plian.system.vo.pm.realproperty.RealPropertyQuoVO;
import com.plian.system.wrapper.pm.realproperty.RealPropertyQuoWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 不动产现状登记信息表 控制器
 *
 * @author BladeX
 * @since 2021-05-07
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/realpropertyquo")
@Api(value = "不动产现状登记信息表", tags = "不动产现状登记信息表接口")
public class RealPropertyQuoController extends BaseController {

    private final IRealPropertyQuoService realPropertyQuoService;

    private RealPropertyQuoWrapper realPropertyQuoWrapper;

    /**
     * 详情
     */
    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入realPropertyQuo")
    public R<RealPropertyQuoVO> detail(@RequestBody RealPropertyQuo realPropertyQuo) {
        RealPropertyQuo detail = realPropertyQuoService.getOne(Condition.getQueryWrapper(realPropertyQuo));
        return R.data(realPropertyQuoWrapper.entityToVO(detail));
    }

    /**
     * 分页 不动产现状登记信息表
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页", notes = "传入query")
    public ListResult<RealPropertyQuoVO> list(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs() == null && query.getDescs() == null) {
            query.setDescs("create_time");
        }
        QueryWrapper<RealPropertyQuo> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new RealPropertyQuo()), entityList
        );
        TokenSearch<RealPropertyQuo> tokenSearch = new TokenSearch<>();
        tokenSearch.addTokenQuery(queryWrapper, query);

        IPage<RealPropertyQuo> pages = realPropertyQuoService.page(Condition.getPage(query), queryWrapper);
        ListResult<RealPropertyQuoVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(realPropertyQuoWrapper.entityToVO(pages.getRecords()));
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());

        return listResult;
    }

    /**
     * 新增或修改 不动产现状登记信息表
     */
    @PostMapping("/submit")
    @LogOperation("不动产现状登记信息-提交")
    @Resubmit(delaySeconds = 0)
    @ApiOperation(value = "新增或修改", notes = "传入realPropertyQuo")
    public R submit(@Valid @RequestBody RealPropertyQuo realPropertyQuo) {
        String message = realPropertyQuoService.checkYear(realPropertyQuo);
        if (StringUtil.isNotBlank(message)) {
            return R.fail(message);
        }
        return R.status(realPropertyQuoService.submit(realPropertyQuo));
    }


    /**
     * 删除 不动产登记信息表
     */
    @PostMapping("/remove")
    @LogOperation("不动产现状登记信息-删除")
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>) pd.get("ids");
        return R.status(realPropertyQuoService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA))));
    }
}
